﻿define([
// dojo    
        "dojo/_base/declare",
        "dojo/_base/array",
        "dojo/_base/lang",
        "dojo/_base/connect",
        "dojo/dom-class",
// dijit
        "dijit/registry",
        "xstyle/css!./css/extendedContentAreaEditor.css"
    ],
    function (
        declare,
        array,
        lang,
        connect,
        domClass,
        registry) {

        return declare("ow.epi.extendedContentAreaEditor.contentAreaValidator", [], {

            _contentAreaValidatorHandlers: null,
            _nonSupportedNode: null,

            postCreate: function () {
                this.inherited(arguments);

                var nonSupportedNodeId = registry.getUniqueId('messageNode');
                var supportedTypeNames = [];
                for (var i = 0; i < this.supportedTypes.length; i++) {
                    supportedTypeNames.push(this.supportedTypes[i].name);
                }
                var nonSupportedText = 'This content area only accepts content of the following types: ' + supportedTypeNames.join(', ');
                this._nonSupportedNode = dojo.create('span', { id: nonSupportedNodeId, 'class': 'not-supported-type' }, this.domNode, "last");
                dojo.create('span', { innerHTML: nonSupportedText }, this._nonSupportedNode, "last");

                dojo.addClass(this.domNode, 'extended-content-area');
                this._contentAreaValidatorHandlers = [];
                this._contentAreaValidatorHandlers.push(connect.subscribe("/dnd/start", this, this._onStartDrag));
                this._contentAreaValidatorHandlers.push(connect.subscribe("/dnd/drop", this, this._onEndDrag));
                this._contentAreaValidatorHandlers.push(connect.subscribe("/dnd/cancel", this, this._onEndDrag));
            },

            _onStartDrag: function (source, nodes, copy) {
                domClass.toggle(this._nonSupportedNode, "active", !this.isValidDndType(source, nodes));
            },

            _onEndDrag: function () {
                domClass.remove(this._nonSupportedNode, "active");
            },

            isValidDndType: function (source, nodes) {
                if (nodes.length > 0) {
                    var dndData = source.getItem(nodes[0].id).data;
                    // D&D from other lists
                    if (dndData) {

                        // for pages contentTypeID is in nested dndData object
                        if (dndData.dndData) {
                            dndData = dndData.dndData;
                        }

                        // if contentTypeID is not recognized then exit
                        if (!dndData.contentTypeID) {
                            return false;
                        }

                        for (var i = 0; i < this.supportedTypes.length; i++) {
                            if (this.supportedTypes[i].iD === dndData.contentTypeID) {
                                return true;
                            }
                        }
                        return false;
                    } else {
                        // sorting
                        return this.id === source.id;
                    }
                }
                return true;
            }

            /*,
            destroy: function () {
            if (this._contentAreaValidatorHandlers) {
            for (var i = 0; i < this._contentAreaValidatorHandlers.length; i++) {
            connect.unsubscribe(this._contentAreaValidatorHandlers[i]);
            }
            delete this._subscribeHandlers;
            }
            }*/
        });
    });